$(function(){
	$('#goodsTypeTree').tree({
		url: js+'dict/selectTreeRootGoodsType.action',
		onBeforeExpand:function(node,param){
            $('#goodsTypeTree').tree('options').url = js+'dict/selectTreeNodeGoodsType.action?goodsTypeID='+node.id;                      
        },
        onBeforeLoad:function(node, param){ 
		}, 
		onLoadSuccess:function(node, data){ 
			
		},
		onClick:function(node){ 
			$(this).tree('expand', node.target);
			$(window.frames["content"].document).find("#parentID").val(node.id);
			content.reloadTable();
		}
		
	});	
	
	$('#addGoodsTypeDialog').dialog({
		toolbar:[{
			text:'保存',
			iconCls:'icon-save',
			handler:function(){
				var node = $('#goodsTypeTree').tree('getSelected');
				$("#parentGoodsTypeID").val(node.id);
				$('#addGoodsTypeForm').form('submit',{
	    				url: url,
	    				onSubmit: function(){
	    					 if($(this).form('validate')){
	    					 	return true;
	    					 }else{
	    					 	$.messager.alert('提示：','请填写完整信息','error');
	    					 	return false;
	    					 }
	    				},
	    				success: function(result){
	    					var result = eval('('+result+')');
	    					if (result.success){
	    					
	    						$('#addGoodsTypeDialog').dialog('close');
	    						var node = $('#goodsTypeTree').tree('getSelected');
								if(node==null){
									node = $('#goodsTypeTree').tree('getRoot');
								}
								if(kind=='add'){
									var goodsTypeName=$("#goodsTypeName").val();
									$('#goodsTypeTree').tree('append',{
										parent: (node?node.target:null),
										data:[{
											id:result.goodsTypeID,
											text:goodsTypeName
										}]
									});
								}
								var value=$('#search-input').val();
								$(window.frames["content"].document).find("#parentID").val(node.id);
								$(window.frames["content"].document).find("#searchInput").val(value);
								content.reloadTable();
								$('#addGoodsTypeForm').form('clear');
	    					} else {
    							$.messager.alert('提示：','货物类型编号重复','error');
	    					}
	    				}
	    		});
			
			
				
				
			}},'-',
			{
			text:'取消',
			iconCls:'icon-cancel',
			handler:function(){
				$('#addGoodsTypeDialog').dialog('close');
			}
		}]
	});
	
	$('#add').click(function(){
		kind='add';
		url=js+'dict/addGoodsType.action';
		$('#addGoodsTypeDialog').dialog('open').dialog('setTitle',' 新增货物类型');
		return false;
	})
	$('#mul-delete').click(function(){
		window.frames["content"].mulDelete();
		return false;
	})
})

function removeTreeNode(id){
	$('#goodsTypeTree').tree('remove',$('#goodsTypeTree').tree('find',id).target);
}
function update(rowData){
	kind='update';
	$('#addGoodsTypeDialog').dialog('open').dialog('setTitle','修改货物类型');
	$('#addGoodsTypeDialog').form('load',rowData);
	url = js+'dict/updateGoodsType.action'
}

function lookup(value,name){
	if('current'==name){
		var node = $('#goodsTypeTree').tree('getSelected');
		if(node==null){
			node = $('#goodsTypeTree').tree('getRoot');
		}
		$(window.frames["content"].document).find("#parentID").val(node.id);
		$(window.frames["content"].document).find("#searchInput").val(value);
		content.reloadTable();
	}
}
